package me.muci.B_threadMethods.synchronizedTest;

/**
 * ClassName: MyThreadWithSync2 <br/>
 * Description: <br/>
 * date: 2021/8/9 14:23<br/>
 *
 * @author muci<br />
 * @description
 * @since JDK 1.8
 */
public class MyThreadWithSync2 implements Runnable{
    static MyThreadWithSync2 instance = new MyThreadWithSync2();

    Object block1 = new Object();
    Object block2 = new Object();

    @Override
    public void run() {

        synchronized (block1) {
            System.out.println("block1锁,我是线程" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("block1锁,"+Thread.currentThread().getName() + "结束");
        }

        synchronized (block2) {
            System.out.println("block2锁,我是线程" + Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("block2锁,"+Thread.currentThread().getName() + "结束");
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(instance);
        Thread t2 = new Thread(instance);
        t1.start();
        t2.start();
    }
}
